#!/bin/bash
BB=/system/bin/busybox

# add log wait flag featrue
setprop sys.qiku_log_wait "1"

DATE="$BB date"
GREP="$BB grep"
TAIL="$BB tail"
CUT="$BB cut"
SED="$BB sed"
PRINTF="$BB printf"

LOG_DIR="/sdcard/admin"
LOG_DIR_RENAME=
[ -x "$BB" ] || exit

DATA_DIR=
OUT_DIR=
PIDS=

if ! [ -n "$OUT_DIR" -a -d "$OUT_DIR" ]; then
    date=$(date "+%y%m%d_%H%M%S")
    DATA_DIR="ZT_${date}"
    OUT_DIR="$LOG_DIR/$DATA_DIR"
fi

mkdir -p "$OUT_DIR"
setprop persist.sys.backup_more_log "$OUT_DIR"

setprop persist.sys.stop_qiku_log "0"
stop logcat_main
#stop logcat_system
stop logcat_radio
stop logcat_events
#stop logcat_crash
stop logcat_klogd
stop qseelog
start backup_more_log

qxdm=`getprop persist.sys.qiku_log_qxdm`
case "$qxdm" in
    0)
    start qiku_diag_mdlog_stop
    start killdiag
    ;;
esac

net=`getprop persist.sys.qiku_log_net`
case "$net" in
    0)  stop logcat_tcp
    ;;
esac

sync


mkdir -p "$OUT_DIR/app"
mv /data/misc/logd/* $OUT_DIR/app/

mkdir -p "$OUT_DIR/hci"
cp -rf /data/misc/bluetooth/logs/ $OUT_DIR/hci/

mkdir -p "$OUT_DIR/wlan"
cp -rf /sdcard/wlan_logs/ $OUT_DIR/wlan/
rm -rf /sdcard/wlan_logs/*

cp -rf /data/system/dropbox $OUT_DIR
cp -rf /data/tombstones $OUT_DIR
cp -rf /data/anr $OUT_DIR
cp -rf /sys/fs/pstore/ $OUT_DIR

# COPY code register to sdcadadmin
# begin
if [ -f /sys/devices/soc0/soc_id ]; then
    soc_id=`cat /sys/devices/soc0/soc_id`
else
    soc_id=`cat /sys/devices/system/soc/soc0/id`
fi

case "$soc_id" in
    "318" | "327"|"317" | "324" | "325" | "326" | "345" | "346")
        mkdir -p "$OUT_DIR/CODECREG"
        cp /d/regmap/spmi0-03/registers $OUT_DIR/CODECREG/spmi003registers
        cp /d/regmap/msm_digital_codec/registers $OUT_DIR/CODECREG/msmregisters
    ;;
esac
# end

mv $LOG_DIR/net $OUT_DIR/net/
uptime > $OUT_DIR/app/uptime.txt

#MODIFY THE TCPLOG NAME
for tcpname in $( ls $OUT_DIR/net/* );do
    mv $tcpname $tcpname.pcap
done

out_export=`getprop persist.sys.backup_more_log`
while [ $out_export != "null" ]
do
  echo "waitting ..."
  out_export=`getprop persist.sys.backup_more_log`
  echo "$out_export"
done

tar zcvf $OUT_DIR.tar.gz -C /sdcard/admin $DATA_DIR
rm -rf $OUT_DIR/

rm -rf /data/misc/logd/*
rm -rf /data/system/dropbox/*
rm -rf /data/tombstones/*
rm -rf /data/anr/*

# clear log wait flag featrue
setprop sys.qiku_log_wait "0"